package org.darcy.framework.model;

import java.io.Serializable;

import lombok.Getter;
import lombok.Setter;

/**
 * 返回客户端统一格式，包括状态码，提示信息，以及业务数据
 */
@Getter
@Setter
public class ResponseData<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	// 状态码
	private Integer code = 0;
	// 必要的提示信息
	private String msg;
	// 业务数据
	private T data;

	public ResponseData(Integer code, String message) {
		this.code = code;
		this.msg = message;
	}

	public ResponseData(Integer code) {
		this.code = code;
	}

	public ResponseData(String message) {
		this.msg = message;
	}

	public ResponseData(T t) {
		this.data = t;
	}

	public ResponseData() {
	}

	/**
	 * 执行成功
	 */
	public ResponseData<T> ok() {
		ResponseData<T> data = new ResponseData<T>(0, "执行成功");
		return data;
	}

	/**
	 * 执行失败
	 */
	public ResponseData<T> error() {
		ResponseData<T> data = new ResponseData<T>(-1, "执行失败");
		return data;
	}

	/**
	 * 赋值数据
	 */
	public ResponseData<T> data(T t) {
		ResponseData<T> data = new ResponseData<T>(0, "执行成功");
		data.setData(t);
		return data;
	}

}
